﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace YJKRevitBeam
{
	public class BeamFormData
	{
		public List<Element> Levels
		{
			get
			{
				return this.m_levels;
			}
		}

		public List<Element> BeamSymbols
		{
			get
			{
				return this.m_beams;
			}
		}

		public BeamFormData(ExternalCommandData cmdData)
		{
			this.m_cmdData = cmdData;
			this.m_doc = cmdData.Application.ActiveUIDocument.Document;
			this.InitLevelList();
			this.InitBeamTypeList();
		}

		private void InitLevelList()
		{
			foreach (Level level in this.GetSortLevels(this.m_doc, double.MinValue, double.MaxValue))
			{
				if (level != null)
				{
					this.m_levels.Add(level);
				}
			}
		}

		private void InitBeamTypeList()
		{
			foreach (Element element in ElementFiltering.FindSpecialElements(this.m_doc, typeof(FamilySymbol), BuiltInCategory.OST_StructuralFraming))
			{
				FamilySymbol familySymbol = element as FamilySymbol;
				if (familySymbol != null)
				{
					this.m_beams.Add(familySymbol);
				}
			}
		}

		private List<Level> GetSortLevels(Autodesk.Revit.DB.Document doc, double bottom, double top)
		{
			List<Level> list = new List<Level>();
			foreach (Element element in new FilteredElementCollector(doc).OfClass(typeof(Level)).ToElements())
			{
				Level level = element as Level;
				double elevation = level.Elevation;
				if (elevation >= bottom && elevation <= top)
				{
					list.Add(level);
				}
			}
			list.Sort(new LevelComparer());
			return list;
		}

		private ExternalCommandData m_cmdData;

		private Document m_doc;

		private List<Element> m_levels = new List<Element>();

		private List<Element> m_beams = new List<Element>();
	}
}
